#include <iostream>
#include <cstdio>

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int maxn = 5555;

int n;

int sum[maxn << 2];

void PushUp(int rt) {
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void build(int l, int r, int rt) {
	sum[rt] = 0;
	if (l == r) {
		return;
	}
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
}

void update(int p, int l, int r, int rt) {
	if (l == r) {
		sum[rt]++;
		return;
	}
	int m = (l + r) >> 1;
	if (p <= m) update(p, lson);
	else update(p, rson);
	PushUp(rt);
}

int query(int L, int R, int l, int r, int rt) {
	if (L <= l && r <= R) {
		return sum[rt];
	}
	int m = (l + r) >> 1;
	int ret = 0;
	if (L <= m) ret += query(L, R, lson);
	if (R > m) ret += query(L, R, rson);
	return ret;
}

int main() {
	while(scanf("%d", &n) != EOF) {
		build(0, n - 1, 1);
		int x[maxn];

		int ans = 0;
		for (int i = 0; i < n; i++) {
			scanf("%d", &x[i]);
			ans += query(x[i], n - 1, 0, n - 1, 1);
			update(x[i], 0, n - 1, 1);
		}

		int min_ans = ans;
		for (int i = 0; i < n; i++) {
			ans += n - 1 - 2 * x[i];
			min_ans = min(min_ans, ans);
		}
		cout << min_ans << endl;		
	}

}